using System;
using System.Text;
using TccAg.VO;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace TccAg.DA
{
    public class ProfessorDA
    {
        private String queryInsert = "INSERT INTO professor( NOME) VALUES( @nome)";
        private String queryUpdate = "UPDATE professor SET nome = @nome WHERE ID = @id";
        private String queryDelete = "DELETE FROM professor WHERE ID = @id";
        private String queryLoadAll = "SELECT  id, nome FROM professor";
        private String queryLoadByPk = "SELECT  id, nome FROM professor WHERE ID = @id";
        private String queryLoadByDisciplina = 
            @"SELECT  
                id, 
                nome 
              FROM 
                professor 
              JOIN 
                disc_professor on (professor.id = disc_professor.professor) 
             WHERE 
                disc_professor.disciplina = @id";

        private String queryLoadRelevancia =
            @"SELECT  
                relevancia
              FROM 
                disc_professor 
              WHERE 
                disc_professor.disciplina = @idDisciplina
                AND
                disc_professor.professor = @idProfessor";
        private SqlConnection conexao = new SqlConnection("Pegar conexão");

        //INSERIR 
        public int inserir(ProfessorVO professor)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryInsert;

                if (professor.Nome != null)
                        cmd.Parameters.Add(new SqlParameter("@nome", (object) professor.Nome));
                else
                        cmd.Parameters.Add(new SqlParameter("@nome", (object) DBNull.Value));

                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //ATUALIZAR 
        public int atualizar(ProfessorVO professor)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                if (professor.Id == 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)professor.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryUpdate;

                if (professor.Nome != null)
                        cmd.Parameters.Add(new SqlParameter("@nome", (object) professor.Nome));
                else
                        cmd.Parameters.Add(new SqlParameter("@nome", (object) DBNull.Value));
                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //EXCLUIR 
        public int excluir(ProfessorVO professor)
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryDelete;

                if (professor.Id == 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)professor.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                conexao.Open();
                return cmd.ExecuteNonQuery();

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD ALL 
        public List<ProfessorVO> loadAll()
        {
            try
            {
                List<ProfessorVO> objRetorno = new List<ProfessorVO>();
                ProfessorVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadAll;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        objTemp = new ProfessorVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["nome"] != DBNull.Value)
                           objTemp.Nome = (String)dr["nome"];

                        objRetorno.Add(objTemp);
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        public List<ProfessorVO> loadByDisciplina(DisciplinaVO disciplina)
        {
            try
            {
                List<ProfessorVO> objRetorno = null;
                ProfessorVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                if (disciplina.Id == 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)disciplina.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadByPk;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    {
                        objTemp = new ProfessorVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["nome"] != DBNull.Value)
                           objTemp.Nome = (String)dr["nome"];

                        objRetorno.Add(objTemp);
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //LOAD BY FK - Passa um objeto do tipo VO para em casos onde usar chave estrangeira composta ter essas propriedades preenchidas
        public List<ProfessorVO> loadByFk(ProfessorVO professor)
        {
            try
            {
                List<ProfessorVO> objRetorno = new List<ProfessorVO>();
                ProfessorVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadByDisciplina;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    {
                        objTemp = new ProfessorVO();
                        if (dr["id"] != DBNull.Value)
                           objTemp.Id = (int)dr["id"];

                        if (dr["nome"] != DBNull.Value)
                           objTemp.Nome = (String)dr["nome"];

                        objRetorno.Add(objTemp);
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        //DATATABLE LOAD ALL 
        public DataTable dataTableLoadAll()
        {
            try
            {
                SqlCommand cmd;
                cmd = conexao.CreateCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadAll;

                conexao.Open();
                SqlDataAdapter dtAdpt = new SqlDataAdapter(cmd);
                DataTable dtTbl = new DataTable("tab_professor");

                dtAdpt.Fill(dtTbl);
                return dtTbl;

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        public int obterRelevancia(ProfessorVO professor, DisciplinaVO disciplina)
        {
            try
            {                
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                if (disciplina.Id == 0)
                    cmd.Parameters.Add(new SqlParameter("@idDisciplina", (object)disciplina.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                if (professor.Id == 0)
                    cmd.Parameters.Add(new SqlParameter("@idProfessor", (object)professor.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");


                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadByPk;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    {
                        ProfessorVO objTemp = new ProfessorVO();
                        if (dr["relevancia"] != DBNull.Value)
                            return (int)dr["relevancia"];                       
                    }
                }
                return -1;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }

        public ProfessorVO loadByPk(int id)
        {
            try
            {
                ProfessorVO objRetorno = new ProfessorVO();
                objRetorno.Id = id;
                return loadByPk(objRetorno);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }
        
        //LOAD BY PK - Passa um objeto do tipo VO para em casos onde usar chave primária composta ter essas propriedades preenchidas
        public ProfessorVO loadByPk(ProfessorVO professor)
        {
            try
            {
                ProfessorVO objRetorno = null;
                ProfessorVO objTemp;
                SqlCommand cmd;
                SqlDataReader dr;
                cmd = conexao.CreateCommand();
                if (professor.Id == 0)
                    cmd.Parameters.Add(new SqlParameter("@id", (object)professor.Id));
                else
                    throw new Exception("Parâmetro necessário não enviado para o DA");

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = queryLoadByPk;

                conexao.Open();
                dr = cmd.ExecuteReader();

                if (dr.HasRows)
                {
                    dr.Read();
                    {
                        objTemp = new ProfessorVO();
                        if (dr["id"] != DBNull.Value)
                            objTemp.Id = (int)dr["id"];

                        if (dr["nome"] != DBNull.Value)
                            objTemp.Nome = (String)dr["nome"];

                        objRetorno = objTemp;
                    }
                }
                return objRetorno;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (conexao.State == System.Data.ConnectionState.Open) conexao.Close();
            }
        }



    }
}
